package com.haixiaoke.saas.enterprise.material.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.material.domain.MaterialGroup;
import com.haixiaoke.saas.material.service.IMaterialGroupService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.saas.common.Node;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 素材组Controller
 * 
 * @author panda
 * @date 2023-12-25
 */
@RestController
@RequestMapping("/enterprise/material/group")
public class MaterialGroupController extends BaseController {
    @Autowired
    private IMaterialGroupService materialGroupService;

    /**
     * 查询素材组列表
     */
    @PreAuthorize("@ss.hasPermi('business:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(MaterialGroup materialGroup) {
        startPage();
        List<MaterialGroup> list = materialGroupService.selectMaterialGroupList(materialGroup);
        return getDataTable(list);
    }

    /**
     * 查询该租户下的素材组树状结构
     * @param materialGroup 查询条件
     * @return  树状结构
     */
    @GetMapping("/tree/list")
    @TenantScope
    public List<Node> treeList(MaterialGroup materialGroup){
        materialGroup.setTenantId(TenantContext.getTenantId());
        return materialGroupService.selectGroupTreeList(materialGroup);
    }

    /**
     * 导出素材组列表
     */
    @PreAuthorize("@ss.hasPermi('business:group:export')")
    @Log(title = "素材组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialGroup materialGroup) {
        List<MaterialGroup> list = materialGroupService.selectMaterialGroupList(materialGroup);
        ExcelUtil<MaterialGroup> util = new ExcelUtil<MaterialGroup>(MaterialGroup.class);
        util.exportExcel(response, list, "素材组数据");
    }

    /**
     * 获取素材组详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:group:query')")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId) {
        return success(materialGroupService.selectMaterialGroupByGroupId(groupId));
    }

    /**
     * 新增素材组
     */
    @PreAuthorize("@ss.hasPermi('business:group:add')")
    @Log(title = "素材组", businessType = BusinessType.INSERT)
    @PostMapping
    @TenantScope
    @RepeatSubmit
    public AjaxResult add(@RequestBody MaterialGroup materialGroup) {
        materialGroup.setTenantId(TenantContext.getTenantId());
        materialGroup.setCreateBy(getUsername());
        return toAjax(materialGroupService.insertMaterialGroup(materialGroup));
    }

    /**
     * 修改素材组
     */
    @PreAuthorize("@ss.hasPermi('business:group:edit')")
    @Log(title = "素材组", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@RequestBody MaterialGroup materialGroup) {
        materialGroup.setUpdateBy(getUsername());
        return toAjax(materialGroupService.updateMaterialGroup(materialGroup));
    }

    /**
     * 删除素材组
     */
    @PreAuthorize("@ss.hasPermi('business:group:remove')")
    @Log(title = "素材组", businessType = BusinessType.DELETE)
	@DeleteMapping("/{groupIds}")
    @TenantScope
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] groupIds) {
        if (!TenantContext.getTenantId().equals(getLoginUser().getTenantId())) {
            return error("异常操作数据！");
        }
        if (groupIds == null || groupIds.length == 0) {
            return error("请选择数据");
        }
        MaterialGroup group = new MaterialGroup();
        group.setUpdateTime(DateUtil.date());
        group.setUpdateBy(getUsername());
        group.setGroupIds(groupIds);
        return toAjax(materialGroupService.softDeleteMaterialGroupByGroupIds(group));
    }
}
